import _ from 'lodash';
import requestApi from '../requestApi'

export default function (props, meta) {
	let pageInfo = props.transferTable.getTablePageInfo(window.InvoiceWalletAreaCode.billsArea);
	let data = {
		querycondition: props.search.getAllSearchData(window.InvoiceWalletAreaCode.searchArea),
		pagecode: window.invoicePageCode.searchPage,
		pageInfo:{
			"pageIndex":pageInfo.pageIndex || 0,
			"pageSize": pageInfo.pageSize || 10,
		},
		queryAreaCode: window.InvoiceWalletAreaCode.searchArea, //查询区编码
		oid: meta[window.InvoiceWalletAreaCode.searchArea].oid, //pub_area 中PK_AREA
		querytype: 'tree'
	}
	let bxdata = window.invoiceListQueryParam

	//左侧票袋列表参数
	const activeId = this.ticketBagActiveId
	let ticketBagType
	if (activeId === 'all') {
		ticketBagType = 0
	} else if (activeId === 'other') {
		ticketBagType = 1
	} else {
		ticketBagType = 2
	}
	const pk_erminvoice_bag = {
		type: ticketBagType
	}
	if (ticketBagType === 2) {
		pk_erminvoice_bag['value'] = activeId
	}
	// debugger
	// 请求添加表体已关联发票的过滤
	let senddata = {
		bxdata,
		data,
		pagecode: window.presetVar.pagecode,
		tepmid: props.meta.getMeta().pageid,
		purchaserstatus: this.state.navTabStatus || '33'
	}
	const { getCardData } = this.props
	const tableData = getCardData() || []
	const selectedInvoiceList = []
	for (let row of tableData) {
		if (row && row.values && row.values.pk_erminvoice 
			// && row.values.pk_erminvoice._display_ !== undefined
			&& row.values.pk_erminvoice.value) {
			
			selectedInvoiceList.push({
				pk_erminvoice: row.values.pk_erminvoice.value,
				generatetype: row.values.generatetype && row.values.generatetype.value
			})
		}
	}

	if(this.state.navTabStatus === '33') {
		senddata['pk_erminvoice_bag']  = pk_erminvoice_bag
	}
	senddata['selectedData'] = selectedInvoiceList;
	requestApi.getWalletList({
		data: senddata,
		success: res => {
			if(!(_.isEmpty(this.state.transfertableMain) && this.state.transfertableMain === null)){
				setStyle(this,props, meta);
			}
			const pageInfo = _.get(res, 'data.pageInfo', {});
			const data = _.get(res, 'data.data', []);
			if (data) {
				// setMainTableStyle(this, {});
				//先取消勾选
				this.props.transferTable.cancelRowsChecked('head', Object.keys(this.deleteList))
				this.deleteList = {}

				this.setTransferBtnDisabled(false)
				data.forEach(item => {
					if (!item.body) {
						item.body = { body: { areacode: 'body', rows: [] } }
					}
				})
				const setData = {
					rows: data,
					pageInfo
				};
				if (pageInfo.pageIndex == 0) {
					setData.allpks = new Array(Number(pageInfo.total));
				}
				this.invoiceList = data
				data && this.props.transferTable.setTransferTableValue(
					window.InvoiceWalletAreaCode.billsArea,
					window.InvoiceWalletAreaCode.bodysArea,
					setData,
					'pk_erminvoice',
					'pk_erminvoice_b'
				)
				if((_.isEmpty(this.state.transfertableMain) && this.state.transfertableMain === null)){
					setStyle(this,props, meta);
				}
			} else {
				// this.setTransferBtnDisabled(true);
				this.props.transferTable.setTransferTableValue(
					window.InvoiceWalletAreaCode.billsArea,
					window.InvoiceWalletAreaCode.bodysArea,
					[],
					'pk_erminvoice',
					'pk_erminvoice_b'
				)
				setStyle(this,props, meta);
			}
			
		},
		error: err => {
			setTimeout(() => {
				setStyle(this,props, meta);
			},100);
		}
	})

	
}

/**
 * @功能 设置样式
 * @作者 Mr.sha
 * @日期 2022-07-07
 * @param {*} props 
 * @param {*} meta 
 */
function setStyle(that,props, meta) {
	let transfertableMain = document.getElementsByClassName('transfertable-main');
	let transfertableMainHeightNum = 0;
	if(!_.isEmpty(transfertableMain) && transfertableMain.length > 0){
		if(_.isEmpty(that.state.transfertableMain) && that.state.transfertableMain === null){
			transfertableMainHeightNum = Number(transfertableMain[0].style.height.replace('px',''));
		}
		else{
			transfertableMainHeightNum = that.state.transfertableMain;
		}
		that.setState({transfertableMain: transfertableMainHeightNum});
		setStyleEleArr(transfertableMain ,'height' , transfertableMainHeightNum + "px");
		
	}
	let wuiTableBody = document.getElementsByClassName('wui-table-body');
	setStyleEleArr(wuiTableBody ,'height' , (transfertableMainHeightNum - 114) + 'px');
	let wuiTableBodyInner = document.getElementsByClassName('wui-table-body-inner');
	setStyleEleArr(wuiTableBodyInner ,'height' , (transfertableMainHeightNum - 114) + 'px');
	setStyleEleArr(wuiTableBodyInner ,'min-height' , (transfertableMainHeightNum - 114) + 'px');
	setStyleEleArr(wuiTableBodyInner ,'max-height' , (transfertableMainHeightNum - 114) + 'px');
	let wuiTableBodyOuter = document.getElementsByClassName('wui-table-body-outer');
	setStyleEleArr(wuiTableBodyOuter ,'height' , (transfertableMainHeightNum - 114) + 'px');
	let baseTableEmptyText = document.getElementsByClassName('base-table-empty-text');
	setStyleEleArr(baseTableEmptyText ,'top' , '180px');
	let transferTablePaginationWrapper = document.getElementsByClassName('transfer-table-pagination-wrapper');
	setStyleEleArr(transferTablePaginationWrapper ,'height' , '42px');
	
}

/**
 * @功能 设置样式~~~对象数组
 * @作者 Mr.sha
 * @日期 2022-07-07
 * @param {*} element
 * @param {*} property 属性
 * @param {*} value 值
 */
function setStyleEleArr(element ,property , value){
	if(!_.isEmpty(element)  && element.length > 0){
		for(let i = 0 ;i < element.length ;i++){
			setStyleProperty(element[i] ,property , value)
		}
	}
}


/**
 * @功能 设置样式~~~具体到属性
 * @作者 Mr.sha
 * @日期 2022-07-07
 * @param {*} object 对象
 * @param {*} property 属性
 * @param {*} value 值
 */
function setStyleProperty(object ,property , value){
	object.style[property] = value;
}


// /**
//  * @功能: 发票移动到票袋
//  * @作者：Mr.sha
//  * @日期：2022-06-06
//  * @param {*} that 组件对象
//  * @param {*} params 其他参数（暂时没有）
//  */
//  function setMainTableStyle(that, params) {
// 	 setTimeout(() => {
// 		let height = "";
// 		let areaContentMain = document.getElementsByClassName("transfertable-main");
// 		let style = null;
// 		if(areaContentMain && areaContentMain.length > 0 &&  areaContentMain[0].style){
// 				height = areaContentMain[0].style.height;
// 				height = height.replace("px","");
// 				height = Number(height);
// 				// 存在默认高度则不赋值
// 				if(!that.state.initHeight){
// 					that.setState({initHeight: height});
// 				}
// 		}
// 	 },2000);

// }
